package com.colorgame.data;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.StreamCorruptedException;
import java.util.ArrayList;

import android.app.Activity;
import android.app.ActionBar.LayoutParams;
import android.view.Gravity;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;

public class StatisticList {
	
	private ArrayList<Statistic> statistic;

	final Activity activity;
	
	public StatisticList(Activity a) {
		activity = a;
		statistic = new ArrayList<Statistic>();
	}
	
	public Statistic newStatistic() {
		Statistic s = new Statistic();
		
		statistic.add(s);
		
		return s;
	}
	

	public void write(final TableLayout tl) {
		tl.removeAllViews();

		addHeader(tl);
		
		for (int i = 1; i <= statistic.size(); i++) {
			TableRow row = new TableRow(activity);

			TextView[] t = new TextView[8];
			
			Statistic stat = statistic.get(i-1);
			
			for (int j = 0; j < t.length; j++) {
				t[j] = new TextView(activity);
				t[j].setGravity(Gravity.RIGHT);
				t[j].setWidth(90);
			}

			
			t[0].setText(""+i);
			t[1].setText(""+stat.hits_gesamt);
			t[2].setText(""+stat.hits_richtig);
			t[3].setText(""+stat.hits_falsch);
			t[4].setText(""+stat.combo_gesamt);
			t[5].setText(""+stat.combo_max);
			t[6].setText(""+stat.combo_neg);
			t[7].setText(""+stat.punkte);
			
//			 t1.setBackgroundColor(Color.BLACK);

			for (int j = 0; j < t.length; j++) {
				row.addView(t[j]);
			}

			tl.addView(row, new TableLayout.LayoutParams(
					LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));

		}
	}
	
	private void addHeader(final TableLayout tl) {
		TableRow row = new TableRow(activity);

		TextView[] t = new TextView[8];
		
		
		for (int j = 0; j < t.length; j++) {
			t[j] = new TextView(activity);
			t[j].setGravity(Gravity.RIGHT);
			t[j].setWidth(90);
		}

		
		t[0].setText("#");
		t[1].setText("HG");	//hits gesamt
		t[2].setText("HR"); //hits richtig
		t[3].setText("HF"); //hits falsch
		t[4].setText("CG"); //combo gesamt
		t[5].setText("CM"); //combo max
		t[6].setText("CN"); //combo negativ
		t[7].setText("P");  //punkte
		
//		 t1.setBackgroundColor(Color.BLACK);

		for (int j = 0; j < t.length; j++) {
			row.addView(t[j]);
		}

		tl.addView(row, new TableLayout.LayoutParams(
				LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
	}



	public void save() {
		write(activity.getDir("\\colorgame\\", activity.MODE_PRIVATE) + "statistic.txt" );
	}

	public void load() {
		final String path = activity.getDir("\\colorgame\\", activity.MODE_PRIVATE) + "statistic.txt";
		if (new File(path).exists()) {
			read(path);		
		} else {
			write(path);
		}
	}

	private void write(final String filename) {
		try {
			FileOutputStream fs = new FileOutputStream(filename);
			ObjectOutputStream os = new ObjectOutputStream(fs);
			os.writeObject(statistic);
			os.close();
		} catch (FileNotFoundException e) { 
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	private void read(final String filename) {
		try {
			FileInputStream fs = new FileInputStream(filename);
			ObjectInputStream is = new ObjectInputStream(fs);
			statistic.clear();
			statistic = (ArrayList<Statistic>) is.readObject();
		} catch (StreamCorruptedException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
	}	

}
